#!/bin/bash
# Copyright (c) 2017, The MesaLock Linux Project Contributors
# All rights reserved.
#
# This work is licensed under the terms of the BSD 3-Clause License.
# For a copy, see the LICENSE file.

set -e

workdir="$(pwd)"
builddir="$(pwd)/build"
rootfsdir="$builddir/rootfs"
isodir="$builddir/iso"

rm -rf "$isodir" && mkdir -p "$isodir"

echo "[+] mesalockiso: installing bootloader and kernel"
packages=(linux syslinux)

for p in "${packages[@]}"; do
  tar xkfJ "$builddir/out/$p.tar.xz" -C "$isodir" &
done
wait

echo "[+] mesalockiso: installing rootfs"
packages=(filesystem \
          minit \
          mgetty \
          glibc \
          gcc-libs \
          ion-shell \
          uutils-coreutils \
          giproute2 \
          mesalock-demo \
         )
          # ripgrep \
          # fd-find \
          # tzdata \
          # xi-core \
          # xi-tui \
          # uutils-findutils \
          # tokei \
          # brotli \
          # micro)

rm -rf "$rootfsdir" && mkdir -p "$rootfsdir"

for p in "${packages[@]}"; do
  tar xfkJ "build/out/$p.tar.xz" -C "$rootfsdir"
done

echo "[+] mesalockiso: packaging initramfs"
mkdir -p "$isodir/boot"
cd "$rootfsdir"
find . | sort | fakeroot cpio --quiet -o -H newc | pigz -9 > "$isodir/boot/initramfs"

echo "[+] mesalockiso: generating iso"
cd "$builddir" && xorrisofs -o mesalock-linux.iso -l -J -R \
	-b boot/syslinux/isolinux.bin \
	-c boot/syslinux/boot.cat \
	-no-emul-boot -boot-load-size 4 -boot-info-table \
	-quiet -follow-links iso \
	> /dev/null
isohybrid mesalock-linux.iso > /dev/null
